﻿@using Smartstore.Core.Web;
@using Smartstore.Utilities;

@model ChoiceModel

@inject IWebHelper WebHelper

@{
    var storeUrl = WebHelper.GetStoreLocation();
    var controlId = Model.BuildControlId();
    var clientId = "upload" + CommonHelper.GenerateRandomInteger();
    var containerId = clientId + "container";
    var allowedFileExtensions = string.Join("|", Model.AllowedFileExtensions.Select(x => x.Trim()).ToList());
    Model.CustomProperties.TryGetValue("UploadedFileInfo", out var file);
}

<div id="@containerId">
    <div class="fu-container">
        <div class="dropzone-container">
            <div class="fu-thumb fu-filename rounded" data-current-filename="@Model.UploadedFileName"></div>
            <input id="@controlId" name="@(controlId)-file" type="hidden" class="hidden skip-pd-ajax-update" data-fileupload="true" value="@Model.UploadedFileGuid" />
            <div class="fu-controls">
                <file-uploader
                    file-uploader-name="@clientId"
                    upload-url="@Model.GetFileUploadUrl(Url)"
                    display-browse-media-button="false"
                    type-filter="@allowedFileExtensions"
                    display-remove-button="@Model.UploadedFileGuid.HasValue()"
                    display-remove-button-after-upload="true"
                    upload-text="@T("Common.FileUploader.Upload")"
                    onuploadcompleted="@("onFileUploaded" + clientId)"
                    onfileremoved="@("onFileRemoved" + clientId)"
                    multi-file="false"
                    has-template-preview="true"
                    download-enabled="true" />
            </div>
        </div>

        <div class="fu-progress">
            <!-- The single upload progress bar -->
            <div class="progress">
                <div class="progress-bar" 
                     role="progressbar" 
                     style="width: 0%" 
                     aria-valuemin="0" 
                     aria-valuemax="100"
                     aria-label="@(Model.UploadedFileName.NullEmpty() ?? T("Aria.Label.UploaderProgressBar", Model.Name))"></div>
            </div>
        </div>
    </div>
</div>

<script data-origin="choice-template">
    window['onFileUploaded@(clientId)'] = function (file, result, progress) {
        if (result.success && result.downloadGuid) {
            setUploadedFile(result.downloadGuid, file.name, "@(containerId)");
            displayNotification(result.message, "success");
        }
	};

    window['onFileRemoved@(clientId)'] = function (e, el) {
        $('#@containerId .hidden').val('');
    }

    function setUploadedFile(downloadGuid, fileName, containerId) {
        if (!_.isEmpty(downloadGuid) && downloadGuid !== '0') {
            $('#' + containerId + ' .hidden').val(downloadGuid);

        	var downloadLink = "@storeUrl" + "download/getfileupload/?downloadId=" + downloadGuid;
        	var fileLink = '<a href="' + downloadLink + '" class="fileuploadattribute" rel="nofollow">' + fileName + '</a>';

            $('#' + containerId + ' .fu-message').html(fileLink);
        }
    }

    @if(file != null)
    {
        <text>
            $(function () {
                var fuContainer = $("#@containerId").find(".fu-container");
                displaySingleFilePreview(JSON.parse('@Html.Raw(file)'), fuContainer, { downloadEnabled: true });
            });
        </text>
    }
</script>
